package com.authority.utils;

import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

	private static final String patternDateTime = "yyyy-MM-dd HH:mm:ss";
	// private static final String patternDateMinutes = "yyyy-MM-dd HH:mm";
	private static final String patternMinutes = "HH:mm";
	private static final String patternDate = "yyyy-MM-dd";

	public static String getTimeDifference(Date end, Date start) {

		if (null == end || null == start){
			return "00:00:00";
		}
		
		long s = start.getTime();
		long e = end.getTime();

		return getTimeDiff(e, s);
	}

	public static String getTimeDifference(String end, String start) throws Exception {

		if (null == end || null == start){
			return "00:00:00";
		}
		
		long s = parseStr2DateTime(start).getTime();
		long e = parseStr2DateTime(end).getTime();

		return getTimeDiff(e, s);
	}

	private static String getTimeDiff(long end, long start) {

		int diff = (int) ((end - start) / 1000);

		return sec2Time(diff);
	}

	public static String getTimeDifference(Date end, String start) throws Exception {
		if (null == end || null == start){
			return "00:00:00";
		}

		long s = parseStr2DateTime(start).getTime();
		long e = end.getTime();

		return getTimeDiff(e, s);
	}

	/**
	 * @param end		结束时间
	 * @param start		开始时间
	 * @return
	 * @throws Exception
	 */
	public static String getTimeDifference(String end, Date start) throws Exception {
		
		if (null == end || null == start){
			return "00:00:00";
		}

		long s = start.getTime();
		long e = parseStr2DateTime(end).getTime();

		return getTimeDiff(e, s);
	}

	public static Long getTimeDiffMS(String end, String start) throws Exception {
		return getTimeDiffMS(parseStr2DateTime(end), parseStr2DateTime(start));
	}

	public static Long getTimeDiffMS(String end, Date start) throws Exception {
		return getTimeDiffMS(parseStr2DateTime(end), start);
	}

	public static Long getTimeDiffMS(Date end, String start) throws Exception {
		return getTimeDiffMS(end, parseStr2DateTime(start));
	}

	public static Long getTimeDiffMS(Date end, Date start){
		if (null == end || null == start){
			return null;
		}
		Long res = end.getTime() - start.getTime();
		return res > 0 ? res : 0;
	}

	public static String sec2Time(int time) {

		int hourTimes = time / 3600;
		String timeStr = "";

		if (hourTimes > 0) {
			timeStr += hourTimes + ":";
			time -= hourTimes * 3600;
		} else {
			timeStr = "0:";
		}

		int minTimes = time / 60;
		if (minTimes > 0) {

			time -= minTimes * 60;
			if (minTimes > 9) {
				timeStr += minTimes + ":";
			} else {
				timeStr += "0" + minTimes + ":";
			}
		} else {
			timeStr += "00:";
		}

		if (time > 9) {
			timeStr += time;
		} else {
			timeStr += "0" + time;
		}

		return timeStr;
	}

	public static String parseDate2Str(Date d) {

		if (d == null) {
			return "";
		}
		DateFormat df = new SimpleDateFormat(patternDateTime);
		return df.format(d);
	}

	public static Date parseStr2DateTime(String ds) throws Exception {

		if (StringUtils.isBlank(ds)) {
			return null;
		}
		DateFormat df = new SimpleDateFormat(patternDateTime);
		return df.parse(ds);
	}

	public static String parseDate2StrMin(Date ds) throws Exception {

		if (ds == null) {
			return null;
		}
		DateFormat df = new SimpleDateFormat(patternMinutes);
		return df.format(ds);
	}

	public static Date parseStr2Date(String ds) throws Exception {

		if (StringUtils.isBlank(ds)) {
			return null;
		}

		DateFormat df = new SimpleDateFormat(patternDate);
		return df.parse(ds);
	}
	
	public static Date parseStr2Date(String date,String formatType) throws Exception {

		if (StringUtils.isBlank(date)) {
			return null;
		}

		DateFormat df = new SimpleDateFormat(formatType);
		return df.parse(date);
	}
	
	public static String parseDate2StrDate(Date d) {

		if (d == null) {
			return "";
		}
		DateFormat df = new SimpleDateFormat(patternDate);
		return df.format(d);
	}

}
